package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javafx.geometry.Pos;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 *  启动配置步骤：
 *      1、新建启动类
 *      2、新建配置类
 *          配置要扫描的dao包
 *
 * @author testjava
 * @since 2020-06-20
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/list")
    public R list(){
        List<EduTeacher> list = eduTeacherService.list(null);
        //可以封装成map数据，也可以使用键值对
        return R.ok().data("list",list);
    }

    /**
     * @DeleteMapping 使用该注解时删除无法用浏览器直接测试
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/delete/{id}")
    public R deleteById(@ApiParam(name = "id", value = "讲师ID", required = true)
                                  @PathVariable("id") String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @GetMapping("/page/{page}/{size}")
    public R findByPage(@PathVariable("page") int page,@PathVariable("size") int size){
        Page<EduTeacher> page1=new Page<>(page,size);
        IPage<EduTeacher> teacherIPage = eduTeacherService.page(page1, null);
        long total = teacherIPage.getTotal();
        List<EduTeacher> records = teacherIPage.getRecords();
//        第一种方式，用map集合封装数据
//        Map<String,Object> map=new HashMap<>();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);
//        第二种方式，链式调用
        return R.ok().data("total",total).data("rows",records);
    }

    @PostMapping("/pageTeacherCondition/{page}/{size}")
    public R pageTeacherCondition(@PathVariable("page") int page, @PathVariable("size") int size,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){  //required = false表示不必须
        Page<EduTeacher> teacherPage=new Page<>(page,size);
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(teacherQuery.getName())){
            wrapper.like("name",teacherQuery.getName());
        }
        if(teacherQuery.getLevel()!= null){
            wrapper.eq("level",teacherQuery.getLevel());
        }
        if(!StringUtils.isEmpty(teacherQuery.getBegin())){
            wrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if(!StringUtils.isEmpty(teacherQuery.getEnd())){
            wrapper.le("gmt_create",teacherQuery.getEnd());
        }
        //按保存时间降序排序
        wrapper.orderByDesc("gmt_create");
        IPage<EduTeacher> teacherIPage = eduTeacherService.page(teacherPage, wrapper);
        long total = teacherIPage.getTotal();
        List<EduTeacher> records = teacherIPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @GetMapping("/getTeacherById/{id}")
    public R getTeacherById(@PathVariable("id") String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    /**
     * 根据id修改数据
     * @param eduTeacher
     * 可以使用put请求，不过使用put请求需要用@PathVariable接收一下id，并设置给eduTeacher，不然会有问题
     * @return
     */
    @PostMapping("update")
    public R update(@RequestBody EduTeacher eduTeacher){
        try {
//            int a=10/0;
        }catch (Exception e){
            throw new GuliException(20001,"自定义异常");
        }
        boolean update = eduTeacherService.updateById(eduTeacher);
        if(update){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

